package com.sinoglobal.sinostore.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.alibaba.fastjson.JSON;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.adapter.InvoiceGoodsTypeAdapter;
import com.sinoglobal.sinostore.bean.GoodsTypeListVo;
import com.sinoglobal.sinostore.bean.GoodsTypeListVo.GoodsTypeVo;
import com.sinoglobal.sinostore.utils.TestJson;

/**
 * @author LK
 * @title: InvoiceActivity.java
 * @package com.chiliring.sinostore.activity
 * @description: TODO(发票信息)
 * @date 2015-1-16 下午2:39:22
 */
public class InvoiceActivity extends StateBaseActivity implements OnCheckedChangeListener, OnItemClickListener, OnClickListener {

    /**
     * 发票抬头
     **/
    private RadioGroup rgInvoiceLookUp;

    /**
     * 个人
     **/
    private RadioButton rbPersonal;

    /**
     * 单位
     **/
    private RadioButton rbUnit;

    /**
     * 单位名称
     **/
    private EditText etUniOtName;

    /**
     * 商品类型
     **/
    private ListView lvGoodsType;

    /**
     * 单位名称
     **/
    private LinearLayout llUnitLayout;

    private InvoiceGoodsTypeAdapter invoiceGoodsTypeAdapter;

    /**
     * 发票类型
     **/
    private String invoiceType = "纸质发票";

    /**
     * 发票抬头
     **/
    private int invoiceInt = 1;

    String invoiceLookUp = "";

    /**
     * 商品种类
     **/
    private GoodsTypeVo typeVo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop_activity_invoice);
        titleView.setText("发票信息");
        openChenJInShi();
        initView();

        initData();
    }

    /**
     * @author LK
     * @createdate 2015-3-31 下午2:10:09
     * @Description: (初始化view)
     */
    private void initView() {
        rgInvoiceLookUp = getViewById(R.id.rg_invoice_look_up);
        rbPersonal = getViewById(R.id.rb_personal);
        rbUnit = getViewById(R.id.rb_unit);
        etUniOtName = getViewById(R.id.et_unit_name);
        lvGoodsType = getViewById(R.id.lv_goods_type);
        llUnitLayout = getViewById(R.id.ll_unit_layout);

        rgInvoiceLookUp.setOnCheckedChangeListener(this);
        lvGoodsType.setOnItemClickListener(this);
        templateButtonLeft.setOnClickListener(this);
    }

    /**
     * @author LK
     * @createdate 2015-3-31 下午2:10:30
     * @Description: (初始化数据)
     */
    private void initData() {
        invoiceLookUp = getIntent().getStringExtra("invoiceLookUp");
        switch (SubmitOrderFormActivity.isPersonalOrCompany) {
            case 0:
                rbPersonal.setChecked(true);
                break;
            case 1:
                rbUnit.setChecked(true);
                etUniOtName.setText(invoiceLookUp);
                break;
            default:
                break;
        }
        String invoice = TestJson.getInvoice();
        if (!TextUtils.isEmpty(invoice)) {
            GoodsTypeListVo entity = JSON.parseObject(invoice, GoodsTypeListVo.class);
            invoiceGoodsTypeAdapter = new InvoiceGoodsTypeAdapter(this, entity.entity);
            lvGoodsType.setAdapter(invoiceGoodsTypeAdapter);
        }
        invoiceGoodsTypeAdapter.setSelect(SubmitOrderFormActivity.invoicePosition);
        typeVo = (GoodsTypeVo) invoiceGoodsTypeAdapter.getItem(SubmitOrderFormActivity.invoicePosition);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int checkedRadioButtonId = group.getCheckedRadioButtonId();
        if (checkedRadioButtonId == R.id.rb_personal) {
            llUnitLayout.setVisibility(View.GONE);
            invoiceInt = 1;
            SubmitOrderFormActivity.isPersonalOrCompany = 0;
        } else if (checkedRadioButtonId == R.id.rb_unit) {
            llUnitLayout.setVisibility(View.VISIBLE);
            invoiceInt = 2;
            SubmitOrderFormActivity.isPersonalOrCompany = 1;
        } else {
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        invoiceGoodsTypeAdapter.setSelect(position);
        typeVo = (GoodsTypeVo) invoiceGoodsTypeAdapter.getItem(position);
        SubmitOrderFormActivity.invoicePosition = position;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.shop_title_but_left) {
            setGoodsType();
        } else {
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            setGoodsType();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void setGoodsType() {
        if (null == typeVo) {
            showShortToast("请设置发票信息");
            return;
        }
        if (0 != typeVo.id) {
            switch (invoiceInt) {
                case 1:
                    invoiceLookUp = "个人";
                    break;
                case 2:
                    invoiceLookUp = etUniOtName.getText().toString();
                    if (TextUtils.isEmpty(invoiceLookUp)) {
                        showShortToast("请填写单位名称");
                        return;
                    }
                    break;
                default:
                    break;
            }
            Intent intent = new Intent();
            intent.putExtra("invoiceType", invoiceType);
            intent.putExtra("invoiceLookUp", invoiceLookUp);
            intent.putExtra("goodsTypeName", typeVo.name);
            setResult(SubmitOrderFormActivity.INVOICECODE, intent);
        } else {
            setResult(SubmitOrderFormActivity.INVOICECODE);
            SubmitOrderFormActivity.isPersonalOrCompany = 0;
        }
        this.finish();
    }
}
